/*
 * FrontEndSubView.java
 */

package front.end.subestacao.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;
import java.beans.PropertyVetoException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.tree.DefaultMutableTreeNode;

import org.jdesktop.application.Action;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.TaskMonitor;
import org.jdesktop.swingbinding.JComboBoxBinding;

import front.end.subestacao.exception.TipoFichaException;
import front.end.subestacao.itens.Componente;
import front.end.subestacao.itens.Painel;
import front.end.subestacao.itens.Subestacao;
import front.end.subestacao.nucleo.ModSubEstFacade;
import front.end.subestacao.nucleo.ModSubEstImpl;
import front.end.subestacao.rede.LugarFusao;

/**
 * The application's main frame.
 */
public class FrontEndSubView extends FrameView {

	private ModSubEstFacade modSubEstacao;

	// private Painel painel;
	@SuppressWarnings("unchecked")
	private JComboBoxBinding jCombPaineis;

	private FrontEndSubTree bibliotecaTree;

	

	private Boolean visIconesAtivada = true;

	

	private Boolean realizouAlteracao = false;

	public FrontEndSubView(SingleFrameApplication app,
			ModSubEstFacade modSubEstacao) {

		super(app);
		if(modSubEstacao == null){
			modSubEstacao = new ModSubEstImpl(new Subestacao("nova"), this);
		}

		this.modSubEstacao = modSubEstacao;

		initComponents();
		this.getFrame().setMinimumSize(new Dimension(750, 500));
		this.getFrame().addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {

				confirmaSalvar();

			}
		});

		this.nomeSubestacao.setText("Subestacao "
				+ modSubEstacao.getSubestacao().getNome());

		bibliotecaTree = new FrontEndSubTree(modSubEstacao);

		bibliotecaComponentes.setModel(new javax.swing.tree.DefaultTreeModel(
				bibliotecaTree.getTreeNodeMutableBiblioteca()));
		bibliotecaComponentes.setCellRenderer(new RenderizarTree());

		bibliotecaComponentes.setRowHeight(30);

		jCombPaineis = org.jdesktop.swingbinding.SwingBindings
				.createJComboBoxBinding(
						org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ,
						modSubEstacao.getSubestacao().getPaineis(),
						paineisComboList);
		jCombPaineis.bind();

		// status bar initialization - message timeout, idle icon and busy
		// animation, etc
		ResourceMap resourceMap = getResourceMap();
		int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
		messageTimer = new Timer(messageTimeout, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				statusMessageLabel.setText("");
			}
		});
		messageTimer.setRepeats(false);
		int busyAnimationRate = resourceMap
				.getInteger("StatusBar.busyAnimationRate");
		for (int i = 0; i < busyIcons.length; i++) {
			busyIcons[i] = resourceMap
					.getIcon("StatusBar.busyIcons[" + i + "]");
		}
		busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
				statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
			}
		});
		idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
		statusAnimationLabel.setIcon(idleIcon);
		progressBar.setVisible(false);

		// connecting action tasks to status bar via TaskMonitor
		TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
		taskMonitor
				.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
					public void propertyChange(
							java.beans.PropertyChangeEvent evt) {
						String propertyName = evt.getPropertyName();
						if ("started".equals(propertyName)) {
							if (!busyIconTimer.isRunning()) {
								statusAnimationLabel.setIcon(busyIcons[0]);
								busyIconIndex = 0;
								busyIconTimer.start();
							}
							progressBar.setVisible(true);
							progressBar.setIndeterminate(true);
						} else if ("done".equals(propertyName)) {
							busyIconTimer.stop();
							statusAnimationLabel.setIcon(idleIcon);
							progressBar.setVisible(false);
							progressBar.setValue(0);
						} else if ("message".equals(propertyName)) {
							String text = (String) (evt.getNewValue());
							statusMessageLabel.setText((text == null) ? ""
									: text);
							messageTimer.restart();
						} else if ("progress".equals(propertyName)) {
							int value = (Integer) (evt.getNewValue());
							progressBar.setVisible(true);
							progressBar.setIndeterminate(false);
							progressBar.setValue(value);
						}
					}
				});
	}

	@Action
	public void tornaCompPrincipal() {

		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();
		if (painelSelecionado != null) {
			Set<Componente> compsSelect = modSubEstacao
					.getComponentesSelecionados(painelSelecionado);
			if (compsSelect.size() > 1) {
				JOptionPane.showMessageDialog(null,
						"Selecione Apenas Um Componente.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			} else if (compsSelect.size() == 0) {
				JOptionPane.showMessageDialog(null, "Selecione Um Componente.",
						"Erro", JOptionPane.ERROR_MESSAGE);
			} else {

				Iterator<Componente> it = compsSelect.iterator();

				Componente comp = it.next();
				modSubEstacao.tornarCompPrincipal(painelSelecionado, comp);
				jCombPaineis.refresh();
				paineisComboList.repaint();
				this.realizouAlteracao = true;

			}

		} else {
			JOptionPane.showMessageDialog(null, "Selecione Um Painel.", "Erro",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	@Action
	public void exportarXml() {
		if (verificaDepComps()) {

			JOptionPane.showMessageDialog(null,
					"Existem Problemas de Dependencia.\n", "Erro",
					JOptionPane.ERROR_MESSAGE);

			corrigePrimeiroErroDependencias();

		} else {

			File destino = null;
			
			File localModelagens = new File("./modelagens");
			if(!localModelagens.exists()){
				localModelagens.mkdir();
			}
			javax.swing.JFileChooser jfc = new javax.swing.JFileChooser(
					localModelagens);
			
			

			jfc.setMultiSelectionEnabled(false);
			jfc.setDialogTitle("Selecione a pasta para salvar");
			jfc.setDialogType(JFileChooser.SAVE_DIALOG);
			jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
			jfc.setFileFilter(new FiltroArqXml());
			jfc.setAcceptAllFileFilterUsed(false);
			int resposta = jfc.showSaveDialog(null);

			if (resposta == 0) {
				destino = jfc.getSelectedFile();
			} else {
				System.out.println("Cancelando ...");
			}

			if (destino != null) {

				if (!destino.getName().endsWith(".xml")) {
					String novoNome = destino.getAbsoluteFile() + ".xml";
					destino = new File(novoNome);
				}

				this.modSubEstacao.exportXMLDesignCPN(destino);

				System.out.println(jfc.getSelectedFile().getAbsolutePath()
						.toString());
			}
		}

	}

	@Action
	public void naoImplementada() {

		JOptionPane.showMessageDialog(null,
				"Funcionalidade Ainda Nao Implementada.", "Aviso",
				JOptionPane.INFORMATION_MESSAGE);

	}

	@Action
	public void adicionarPainel() {

		/*
		 * if (this.painel != null) { JOptionPane.showMessageDialog(null,
		 * "Versao Limitada a Um Painel.", "Erro", JOptionPane.ERROR_MESSAGE);
		 * return; }
		 */
		String nome = JOptionPane.showInputDialog(this.getComponent(),
				"Informe o Nome do Painel: ");

		Painel painelAdicionado;

		if (nome != null) {

			if (nome.equals("") || nome.contains(" ") || nome.contains("-")|| nome.contains("_")) {
				JOptionPane.showMessageDialog(null, "Nome Invalido.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			} else {

				Boolean nomeJaExiste = modSubEstacao.existePainelNome(nome);

				if (nomeJaExiste) {
					JOptionPane.showMessageDialog(this.getFrame(),
							"Ja Existe Um Painel Com Esse Nome.", "Erro",
							JOptionPane.ERROR_MESSAGE);

				} else {

					try {

						painelAdicionado = this.modSubEstacao.addPainel(nome);
						jCombPaineis.refresh();

						JInternalFrame internalFrame = modSubEstacao
								.getPainelVisual(painelAdicionado)
								.getRepresPainel(visIconesAtivada);

						jDesktopPane1.add(internalFrame,
								javax.swing.JLayeredPane.DEFAULT_LAYER);

						internalFrame.setMaximum(true);

						paineisComboList.setSelectedItem(painelAdicionado);
						this.realizouAlteracao = true;

					} catch (TipoFichaException e) {
						
						e.printStackTrace();
					} catch (PropertyVetoException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}

		}

	}

	@Action
	public void sair() {
		confirmaSalvar();
		System.exit(0);

	}

	@Action
	public void adicionarComponente() {

		JFrame mainFrame = FrontEndSubApp.getApplication().getMainFrame();

		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();

		if (painelSelecionado == null) {
			JOptionPane.showMessageDialog(mainFrame, "Selecione Um Painel.",
					"Erro", JOptionPane.ERROR_MESSAGE);
			return;
		} else {

			DefaultMutableTreeNode selectedComponenteNode = (DefaultMutableTreeNode) bibliotecaComponentes
					.getLastSelectedPathComponent();
			Componente selectedComponente = null;

			if (selectedComponenteNode == null) {

				JOptionPane.showMessageDialog(mainFrame,
						"Selecione Um Componente Antes.", "Erro",
						JOptionPane.ERROR_MESSAGE);
				return;
			} else {

				try {
					selectedComponente = ((Componente) selectedComponenteNode
							.getUserObject()).geraCopia();

					if (selectedComponente.getEhUnicoPorPainel()) {
						List<Componente> compsNoPainel = painelSelecionado
								.getComponentesPorTipo(selectedComponente
										.getTipo());
						if (!compsNoPainel.isEmpty()) {
							JOptionPane.showMessageDialog(mainFrame,
									selectedComponente.getTipo()
											+ " Deve Ser Unico No Painel.",
									"Erro", JOptionPane.ERROR_MESSAGE);
							return;
						}
					}

				} catch (ClassCastException ex) {
					JOptionPane.showMessageDialog(mainFrame,
							"Selecione Um Componente Antes.", "Erro",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
				bibliotecaComponentes.getSelectionModel().getSelectionPath()
						.getLastPathComponent();

				AdicaoComponenteMod adicaoComponenteMod = new AdicaoComponenteMod(
						"Adicao do componente tipo "
								+ selectedComponente.getTipo(), modSubEstacao,
						selectedComponente.getTipo(), painelSelecionado, null,
						mainFrame, null);

				adicaoComponenteMod.setLocationRelativeTo(mainFrame);

				FrontEndSubApp.getApplication().show(adicaoComponenteMod);

				this.realizouAlteracao = true;

			}

		}

	}
	
	@Action
	public void adicionarComponente(Point2D local) {

		JFrame mainFrame = FrontEndSubApp.getApplication().getMainFrame();

		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();

		if (painelSelecionado == null) {
			JOptionPane.showMessageDialog(mainFrame, "Selecione Um Painel.",
					"Erro", JOptionPane.ERROR_MESSAGE);
			return;
		} else {

			DefaultMutableTreeNode selectedComponenteNode = (DefaultMutableTreeNode) bibliotecaComponentes
					.getLastSelectedPathComponent();
			Componente selectedComponente = null;

			if (selectedComponenteNode == null) {

				JOptionPane.showMessageDialog(mainFrame,
						"Selecione Um Componente Antes.", "Erro",
						JOptionPane.ERROR_MESSAGE);
				return;
			} else {

				try {
					selectedComponente = ((Componente) selectedComponenteNode
							.getUserObject()).geraCopia();

					if (selectedComponente.getEhUnicoPorPainel()) {
						List<Componente> compsNoPainel = painelSelecionado
								.getComponentesPorTipo(selectedComponente
										.getTipo());
						if (!compsNoPainel.isEmpty()) {
							JOptionPane.showMessageDialog(mainFrame,
									selectedComponente.getTipo()
											+ " Deve Ser Unico No Painel.",
									"Erro", JOptionPane.ERROR_MESSAGE);
							return;
						}
					}

				} catch (ClassCastException ex) {
					JOptionPane.showMessageDialog(mainFrame,
							"Selecione Um Componente Antes.", "Erro",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
				bibliotecaComponentes.getSelectionModel().getSelectionPath()
						.getLastPathComponent();

				AdicaoComponenteMod adicaoComponenteMod = new AdicaoComponenteMod(
						"Adicao do componente tipo "
								+ selectedComponente.getTipo(), modSubEstacao,
						selectedComponente.getTipo(), painelSelecionado, null,
						mainFrame, local);

				adicaoComponenteMod.setLocationRelativeTo(mainFrame);

				FrontEndSubApp.getApplication().show(adicaoComponenteMod);

				this.realizouAlteracao = true;

			}

		}

	}

	@Action
	public void adicionarFusao() {
		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();
		Set<Componente> componetesSelecionados = this.modSubEstacao
				.getComponentesSelecionados(painelSelecionado);

		if (componetesSelecionados.size() != 2) {
			JOptionPane.showMessageDialog(null,
					"Informe Exatamente Dois Componentes.", "Erro",
					JOptionPane.ERROR_MESSAGE);
			return;
		}

		Iterator<Componente> it = componetesSelecionados.iterator();

		Componente comp1 = it.next();
		Componente comp2 = it.next();

		LugarFusao lugar1 = comp1.getLugaresFusoes().get(0);
		LugarFusao lugar2 = comp2.getLugaresFusoes().get(0);

		String nome = JOptionPane.showInputDialog("Informe o Nome da Fusao: ");

		if (nome != null) {

			if (nome.equals("")) {
				JOptionPane.showMessageDialog(null, "Nome Invalido.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			} else {

				this.modSubEstacao.addConexaoFusao(nome, painelSelecionado,
						lugar1, lugar2);

			}

		}

	}

	@Action
	public void mostraDeclaracoes() {
		JFrame mainFrame = FrontEndSubApp.getApplication().getMainFrame();
		VisualizacaoFichas visFicha = new VisualizacaoFichas(this.getFrame());

		visFicha.setLocationRelativeTo(mainFrame);

		FrontEndSubApp.getApplication().show(visFicha);

	}

	@Action
	public void ativarPainel() {
		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();
		if (painelSelecionado != null) {
			modSubEstacao.ativarPainel(painelSelecionado);
			this.botPainelAtivo.setEnabled(false);
			this.realizouAlteracao = true;
		}

	}

	@Action
	public void salvar() {

		modSubEstacao.persiste(modSubEstacao.getArquivoSubestacao(),
				visIconesAtivada);
		this.realizouAlteracao = false;

	}

	@Action
	public void salvarComo() {

		modSubEstacao.persiste(null, visIconesAtivada);
		this.realizouAlteracao = false;

	}

	@Action
	public void confirmaSalvar() {

		if (realizouAlteracao) {
			Object[] options = { "Sim", "Nao" };

			int i = JOptionPane.showOptionDialog(null,
					"Deseja Salvar as Alteracoes de: "
							+ modSubEstacao.getSubestacao().getNome() + " ?",
					"Salvar", JOptionPane.YES_NO_OPTION,
					JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

			if (i == JOptionPane.YES_OPTION) {
				salvar();
			}
		}

	}

	@Action
	public void novaModelagem() {
		confirmaSalvar();

		String nome = JOptionPane
				.showInputDialog("Informe o Nome da Subestacao ");
		if (nome != null) {

			if (nome.equals("")) {
				JOptionPane.showMessageDialog(null, "Nome Invalido.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			} else {
				this.modSubEstacao = new ModSubEstImpl(new Subestacao(nome), this);
				this.nomeSubestacao.setText("Subestacao "
						+ modSubEstacao.getSubestacao().getNome());
				jCombPaineis = org.jdesktop.swingbinding.SwingBindings
						.createJComboBoxBinding(
								org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ,
								modSubEstacao.getSubestacao().getPaineis(),
								paineisComboList);
				jCombPaineis.bind();

				jCombPaineis.refresh();
				modSubEstacao.geraPaineisVisuais();

				jDesktopPane1.removeAll();
				jDesktopPane1.repaint();
				this.realizouAlteracao = false;

			}
		}

		paineisComboListActionPerformed(null);

	}

	@Action
	public void abre() throws ClassNotFoundException {
		confirmaSalvar();

		File destino = null;
		
		File localModelagens = new File("./modelagens");
		if(!localModelagens.exists()){
			localModelagens.mkdir();
		}
		javax.swing.JFileChooser jfc = new javax.swing.JFileChooser(
				localModelagens);

		jfc.setMultiSelectionEnabled(false);
		jfc.setDialogTitle("Selecione o Arquivo .mod da Subestacao");
		jfc.setDialogType(JFileChooser.OPEN_DIALOG);
		jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		jfc.setFileFilter(new FiltroArqMod());
		int resposta = jfc.showOpenDialog(FrontEndSubApp.getApplication().getMainFrame());

		if (resposta == 0) {
			destino = jfc.getSelectedFile();
		} else {
			System.out.println("Clicou em cancelar");
		}
		
		if (destino != null && !destino.getName().endsWith(".mod")) {
			String novoNome = destino.getAbsoluteFile() + ".mod";
			destino = new File(novoNome);
		}

		if (destino != null && destino.exists()) {

			ObjectInputStream objectIn;
			try {
				objectIn = new ObjectInputStream(new BufferedInputStream(
						new FileInputStream(destino)));
				Subestacao so = (Subestacao) objectIn.readObject();
				objectIn.close();

				jCombPaineis.unbind();

				jDesktopPane1.removeAll();

				// paineisComboListActionPerformed(null);

				this.modSubEstacao = new ModSubEstImpl(so, this);
				// this.modSubEstacao.setSubestacao(so);

				this.nomeSubestacao.setText("Subestacao "
						+ modSubEstacao.getSubestacao().getNome());

				jCombPaineis = org.jdesktop.swingbinding.SwingBindings
						.createJComboBoxBinding(
								org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ,
								modSubEstacao.getSubestacao().getPaineis(),
								paineisComboList);
				jCombPaineis.bind();

				jDesktopPane1.removeAll();

				jCombPaineis.refresh();
				modSubEstacao.geraPaineisVisuais();

				for (PainelVisual pv : modSubEstacao.getPaineisVisuais()) {

					JInternalFrame internalFrame = pv.getRepresPainel(true);

					jDesktopPane1.add(internalFrame,
							javax.swing.JLayeredPane.DEFAULT_LAYER);

					try {
						internalFrame.setMaximum(true);

						// painelAdicionado.getPainelJung().getRepresPainel().setVisible(true);
					} catch (PropertyVetoException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				modSubEstacao.setArquivoSubestacao(destino);
				this.realizouAlteracao = false;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		paineisComboListActionPerformed(null);

	}

	@Action
	public void alternarVisualizacaoParaIcone() {
		System.out.println("alternarVisualizacaoParaIcone");
		if (!visIconesAtivada) {
			modSubEstacao.atualizaPosVisComps(false);
			this.visIconesAtivada = true;

			jCombPaineis.refresh();
			modSubEstacao.geraPaineisVisuais();

			jDesktopPane1.removeAll();

			for (PainelVisual pv : modSubEstacao.getPaineisVisuais()) {

				JInternalFrame internalFrame = pv
						.getRepresPainel(visIconesAtivada);
				// JInternalFrame internalFrame2 = pv.getRepresPainel(false);

				jDesktopPane1.add(internalFrame,
						javax.swing.JLayeredPane.DEFAULT_LAYER);

				// jDesktopPane1.add(internalFrame2,
				// javax.swing.JLayeredPane.DEFAULT_LAYER);
				// paineisComboList.setSelectedItem(painelAdicionado);

				try {
					internalFrame.setMaximum(true);
					// internalFrame2.setMaximum(true);
					// painelAdicionado.getPainelJung().getRepresPainel().setVisible(true);
				} catch (PropertyVetoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			paineisComboListActionPerformed(null);

		}

	}

	@Action
	public void alternarVisualizacaoParaGrafo() {
		System.out.println("alternarVisualizacaoParaGrafo");
		if (visIconesAtivada) {
			modSubEstacao.atualizaPosVisComps(true);
			this.visIconesAtivada = false;

			jCombPaineis.refresh();
			modSubEstacao.geraPaineisVisuais();

			jDesktopPane1.removeAll();

			for (PainelVisual pv : modSubEstacao.getPaineisVisuais()) {

				JInternalFrame internalFrame = pv
						.getRepresPainel(visIconesAtivada);
				// JInternalFrame internalFrame2 = pv.getRepresPainel(false);

				jDesktopPane1.add(internalFrame,
						javax.swing.JLayeredPane.DEFAULT_LAYER);

				// jDesktopPane1.add(internalFrame2,
				// javax.swing.JLayeredPane.DEFAULT_LAYER);
				// paineisComboList.setSelectedItem(painelAdicionado);

				try {
					internalFrame.setMaximum(true);
					// internalFrame2.setMaximum(true);
					// painelAdicionado.getPainelJung().getRepresPainel().setVisible(true);
				} catch (PropertyVetoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			paineisComboListActionPerformed(null);

		}
	}

	@Action
	public void editaDependencias() {

		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();
		if (painelSelecionado != null) {
			Set<Componente> compsSelect = modSubEstacao
					.getComponentesSelecionados(painelSelecionado);
			if (compsSelect.size() > 1) {
				JOptionPane.showMessageDialog(null,
						"Selecione Apenas Um Componente Para Edicao.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			} else if (compsSelect.size() == 0) {
				JOptionPane.showMessageDialog(null,
						"Selecione Um Componente Para Edicao.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			} else {

				Iterator<Componente> it = compsSelect.iterator();

				Componente comp = it.next();

				editaDependencias(painelSelecionado, comp);

			}
		} else {
			JOptionPane.showMessageDialog(null, "Selecione Um Painel.", "Erro",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	public void editaDependencias(Painel painelSelecionado, Componente comp) {

		if (painelSelecionado != null) {

			if (!comp.getDependencias().isEmpty()) {
				FrontEndSubApp.getApplication().show(
						new AdicaoComponenteDep(
								"Adicao das dependencias do componente "
										+ comp.toString(), modSubEstacao, comp,
								painelSelecionado, null, true));
				this.realizouAlteracao = true;

			} else {
				JOptionPane.showMessageDialog(null,
						"O Componente Nao Tem Dependencias.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			}

		} else {
			JOptionPane.showMessageDialog(null, "Selecione Um Painel.", "Erro",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	@Action
	public Boolean verificaDepComps() {
		for (Painel p : modSubEstacao.getSubestacao().getPaineis()) {
			List<Componente> compsProb = p.getListaCompDepProb();
			if (!compsProb.isEmpty()) {
				return true;
			}
		}
		return false;
	}

	@Action
	public void corrigePrimeiroErroDependencias() {
		for (Painel p : modSubEstacao.getSubestacao().getPaineis()) {
			List<Componente> compsProb = p.getListaCompDepProb();
			if (!compsProb.isEmpty()) {
				Componente primeiroErro = compsProb.get(0);
				editaDependencias(p, primeiroErro);
				return;
			}
		}

	}

	@Action
	public void editaNomeSubestacao() {

		String nome = JOptionPane
				.showInputDialog("Informe o Novo Nome da Subestacao: ");
		if (nome != null) {

			if (nome.equals("")) {
				JOptionPane.showMessageDialog(null, "Nome Invalido.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			} else {
				modSubEstacao.editaNomeSubestacao(nome);
				this.nomeSubestacao.setText("Subestacao "
						+ modSubEstacao.getSubestacao().getNome());
				this.realizouAlteracao = true;

			}
		}

	}

	@Action
	public void excluirPainel() {

		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();
		if (painelSelecionado != null) {

			Object[] options = { "Sim", "Nao" };

			int i = JOptionPane.showOptionDialog(null,
					"Tem Certeza Que Deseja Exluir o Painel "
							+ painelSelecionado.getNome() + " ?", "Excluir",
					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
					null, options, options[0]);

			if (i == JOptionPane.YES_OPTION) {
				PainelVisual pv = modSubEstacao
						.getPainelVisual(painelSelecionado);
				jDesktopPane1.remove(pv.getRepresPainel(true));

				modSubEstacao.excluirPainel(painelSelecionado);

				jCombPaineis.unbind();
				jCombPaineis.bind();

				jCombPaineis.refresh();

				this.realizouAlteracao = true;

				paineisComboListActionPerformed(null);

				jDesktopPane1.repaint();
			}

		} else {
			JOptionPane.showMessageDialog(null, "Selecione Um Painel.", "Erro",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	@Action
	public void editaNomePainel() {

		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();
		if (painelSelecionado != null) {
			String nome = painelSelecionado.getNome();
			nome = JOptionPane
					.showInputDialog("Informe o Novo Nome do Painel: ");
			if (nome != null) {

				if (nome.equals("") || nome.contains(" ") || nome.contains("-")|| nome.contains("_")) {
					JOptionPane.showMessageDialog(null, "Nome Invalido.",
							"Erro", JOptionPane.ERROR_MESSAGE);
				} else {
					if (!nome.equals(painelSelecionado.getNome())) {
						modSubEstacao.editaNomePainel(painelSelecionado, nome);
						jCombPaineis.refresh();
						paineisComboList.repaint();
						this.realizouAlteracao = true;
					}

				}
			}
		} else {
			JOptionPane.showMessageDialog(null, "Selecione Um Painel.", "Erro",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	@Action
	public void editaNomeComponente() {
		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();
		if (painelSelecionado != null) {
			Set<Componente> compsSelect = modSubEstacao
					.getComponentesSelecionados(painelSelecionado);
			if (compsSelect.size() > 1) {
				JOptionPane.showMessageDialog(null,
						"Selecione Apenas Um Componente Para Edicao.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			} else if (compsSelect.size() == 0) {
				JOptionPane.showMessageDialog(null,
						"Selecione Um Componente Para Edicao.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			} else {

				String nome = JOptionPane
						.showInputDialog("Informe o Novo Nome do Componente: ");
				if (nome != null) {

					if (nome.equals("") || nome.contains(" ") || nome.contains("-")|| nome.contains("_")) {
						JOptionPane.showMessageDialog(null, "Nome Invalido.",
								"Erro", JOptionPane.ERROR_MESSAGE);
					} else {

						Iterator<Componente> it = compsSelect.iterator();

						Componente comp = it.next();

						if (!nome.equals(comp.getNome())) {
							Boolean nomeJaExiste = modSubEstacao
									.existeCompNome(nome);

							if (nomeJaExiste) {
								JOptionPane
										.showMessageDialog(
												this.getFrame(),
												"Ja Existe Um Componente Com Esse Nome.",
												"Erro",
												JOptionPane.ERROR_MESSAGE);

							} else {
								modSubEstacao.editaNomeComponente(
										painelSelecionado, comp, nome);
								jCombPaineis.refresh();
								paineisComboList.repaint();
								this.realizouAlteracao = true;
							}
						}

					}
				}
			}
		} else {
			JOptionPane.showMessageDialog(null, "Selecione Um Painel.", "Erro",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	@Action
	public void excluirComponente() {
		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();
		if (painelSelecionado != null) {
			Set<Componente> compsSelect = modSubEstacao
					.getComponentesSelecionados(painelSelecionado);
			if (compsSelect.size() > 1) {
				JOptionPane.showMessageDialog(null,
						"Selecione Apenas Um Componente Para Exclusao.",
						"Erro", JOptionPane.ERROR_MESSAGE);
			} else if (compsSelect.size() == 0) {
				JOptionPane.showMessageDialog(null,
						"Selecione Um Componente Para Exclusao.", "Erro",
						JOptionPane.ERROR_MESSAGE);
			} else {

				Iterator<Componente> it = compsSelect.iterator();

				Componente comp = it.next();
				modSubEstacao.deletaComponente(painelSelecionado, comp);
				this.realizouAlteracao = true;

			}
		} else {
			JOptionPane.showMessageDialog(null, "Selecione Um Componente Para Exclusao.", "Erro",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	@Action
	public void showAboutBox() {
		if (aboutBox == null) {
			JFrame mainFrame = FrontEndSubApp.getApplication().getMainFrame();
			aboutBox = new FrontEndSubAboutBox(mainFrame);
			aboutBox.setLocationRelativeTo(mainFrame);
		}
		FrontEndSubApp.getApplication().show(aboutBox);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        bibliotecaPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        bibliotecaComponentes = new javax.swing.JTree();
        menuRapido = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        botCompPrincipal = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        botPainelAtivo = new javax.swing.JButton();
        jSeparator7 = new javax.swing.JToolBar.Separator();
        jButton11 = new javax.swing.JButton();
        jSeparator9 = new javax.swing.JToolBar.Separator();
        palco = new javax.swing.JScrollPane();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        paineisComboList = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        nomeSubestacao = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuItem4 = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jSeparator8 = new javax.swing.JSeparator();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem9 = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JSeparator();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();
        jSeparator10 = new javax.swing.JSeparator();
        jMenuItem14 = new javax.swing.JMenuItem();
        jMenuItem13 = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        buttonGroup1 = new javax.swing.ButtonGroup();

        mainPanel.setMinimumSize(new java.awt.Dimension(600, 400));
        mainPanel.setName("mainPanel"); // NOI18N

        bibliotecaPanel.setName("bibliotecaPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        bibliotecaComponentes.setDragEnabled(true);
        bibliotecaComponentes.setName("bibliotecaComponentes"); // NOI18N
        jScrollPane1.setViewportView(bibliotecaComponentes);

        javax.swing.GroupLayout bibliotecaPanelLayout = new javax.swing.GroupLayout(bibliotecaPanel);
        bibliotecaPanel.setLayout(bibliotecaPanelLayout);
        bibliotecaPanelLayout.setHorizontalGroup(
            bibliotecaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bibliotecaPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        bibliotecaPanelLayout.setVerticalGroup(
            bibliotecaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bibliotecaPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                .addGap(41, 41, 41))
        );

        menuRapido.setName("menuRapido"); // NOI18N

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("barra"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(front.end.subestacao.ui.FrontEndSubApp.class).getContext().getActionMap(FrontEndSubView.class, this);
        jButton9.setAction(actionMap.get("exportarXml")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(front.end.subestacao.ui.FrontEndSubApp.class).getContext().getResourceMap(FrontEndSubView.class);
        jButton9.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton9.setIcon(resourceMap.getIcon("jButton9.icon")); // NOI18N
        jButton9.setText(resourceMap.getString("jButton9.text")); // NOI18N
        jButton9.setFocusable(false);
        jButton9.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton9.setMaximumSize(new java.awt.Dimension(80, 60));
        jButton9.setMinimumSize(new java.awt.Dimension(80, 55));
        jButton9.setName("jButton9"); // NOI18N
        jButton9.setOpaque(false);
        jButton9.setPreferredSize(new java.awt.Dimension(50, 45));
        jButton9.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton9MouseClicked(evt);
            }
        });
        jToolBar1.add(jButton9);

        jButton10.setAction(actionMap.get("salvar")); // NOI18N
        jButton10.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton10.setIcon(resourceMap.getIcon("jButton10.icon")); // NOI18N
        jButton10.setText(resourceMap.getString("jButton10.text")); // NOI18N
        jButton10.setFocusable(false);
        jButton10.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton10.setMaximumSize(new java.awt.Dimension(80, 60));
        jButton10.setMinimumSize(new java.awt.Dimension(80, 55));
        jButton10.setName("jButton10"); // NOI18N
        jButton10.setOpaque(false);
        jButton10.setPreferredSize(new java.awt.Dimension(50, 45));
        jButton10.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton10MouseClicked(evt);
            }
        });
        jToolBar1.add(jButton10);

        jSeparator4.setName("jSeparator4"); // NOI18N
        jToolBar1.add(jSeparator4);

        jButton1.setAction(actionMap.get("adicionarComponente")); // NOI18N
        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setMaximumSize(new java.awt.Dimension(120, 60));
        jButton1.setMinimumSize(new java.awt.Dimension(100, 55));
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setOpaque(false);
        jButton1.setPreferredSize(new java.awt.Dimension(50, 45));
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jToolBar1.add(jButton1);

        jButton2.setAction(actionMap.get("excluirComponente")); // NOI18N
        jButton2.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton2);

        botCompPrincipal.setAction(actionMap.get("tornaCompPrincipal")); // NOI18N
        botCompPrincipal.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        botCompPrincipal.setIcon(resourceMap.getIcon("botCompPrincipal.icon")); // NOI18N
        botCompPrincipal.setText(resourceMap.getString("botCompPrincipal.text")); // NOI18N
        botCompPrincipal.setFocusable(false);
        botCompPrincipal.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botCompPrincipal.setMaximumSize(new java.awt.Dimension(90, 60));
        botCompPrincipal.setName("botCompPrincipal"); // NOI18N
        botCompPrincipal.setPreferredSize(new java.awt.Dimension(50, 45));
        botCompPrincipal.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botCompPrincipal.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botCompPrincipalMouseClicked(evt);
            }
        });
        jToolBar1.add(botCompPrincipal);

        jSeparator5.setName("jSeparator5"); // NOI18N
        jToolBar1.add(jSeparator5);

        jButton5.setAction(actionMap.get("adicionarPainel")); // NOI18N
        jButton5.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton5.setIcon(resourceMap.getIcon("jButton5.icon")); // NOI18N
        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton5.setMaximumSize(new java.awt.Dimension(90, 60));
        jButton5.setName("jButton5"); // NOI18N
        jButton5.setPreferredSize(new java.awt.Dimension(50, 45));
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton5MouseClicked(evt);
            }
        });
        jToolBar1.add(jButton5);

        jButton6.setAction(actionMap.get("excluirPainel")); // NOI18N
        jButton6.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton6.setIcon(resourceMap.getIcon("jButton6.icon")); // NOI18N
        jButton6.setText(resourceMap.getString("jButton6.text")); // NOI18N
        jButton6.setFocusable(false);
        jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton6.setName("jButton6"); // NOI18N
        jButton6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton6);

        botPainelAtivo.setAction(actionMap.get("ativarPainel")); // NOI18N
        botPainelAtivo.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        botPainelAtivo.setIcon(resourceMap.getIcon("botPainelAtivo.icon")); // NOI18N
        botPainelAtivo.setText(resourceMap.getString("botPainelAtivo.text")); // NOI18N
        botPainelAtivo.setFocusable(false);
        botPainelAtivo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botPainelAtivo.setMaximumSize(new java.awt.Dimension(90, 60));
        botPainelAtivo.setName("botPainelAtivo"); // NOI18N
        botPainelAtivo.setPreferredSize(new java.awt.Dimension(50, 45));
        botPainelAtivo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botPainelAtivo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botPainelAtivoMouseClicked(evt);
            }
        });
        jToolBar1.add(botPainelAtivo);

        jSeparator7.setName("jSeparator7"); // NOI18N
        jToolBar1.add(jSeparator7);

        jButton11.setAction(actionMap.get("mostraDeclaracoes")); // NOI18N
        jButton11.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton11.setIcon(resourceMap.getIcon("jButton11.icon")); // NOI18N
        jButton11.setText(resourceMap.getString("jButton11.text")); // NOI18N
        jButton11.setFocusable(false);
        jButton11.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton11.setMaximumSize(new java.awt.Dimension(90, 60));
        jButton11.setName("jButton11"); // NOI18N
        jButton11.setPreferredSize(new java.awt.Dimension(50, 45));
        jButton11.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton11MouseClicked(evt);
            }
        });
        jToolBar1.add(jButton11);

        jSeparator9.setName("jSeparator9"); // NOI18N
        jToolBar1.add(jSeparator9);

        javax.swing.GroupLayout menuRapidoLayout = new javax.swing.GroupLayout(menuRapido);
        menuRapido.setLayout(menuRapidoLayout);
        menuRapidoLayout.setHorizontalGroup(
            menuRapidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(menuRapidoLayout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 730, Short.MAX_VALUE)
                .addContainerGap())
        );
        menuRapidoLayout.setVerticalGroup(
            menuRapidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
        );

        palco.setName("palco"); // NOI18N

        jDesktopPane1.setBackground(resourceMap.getColor("jDesktopPane1.background")); // NOI18N
        jDesktopPane1.setName("jDesktopPane1"); // NOI18N
        palco.setViewportView(jDesktopPane1);

        paineisComboList.setFont(resourceMap.getFont("paineisComboList.font")); // NOI18N
        paineisComboList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        paineisComboList.setName("paineisComboList"); // NOI18N
        paineisComboList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                paineisComboListActionPerformed(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        nomeSubestacao.setFont(resourceMap.getFont("nomeSubestacao.font")); // NOI18N
        nomeSubestacao.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        nomeSubestacao.setText(resourceMap.getString("nomeSubestacao.text")); // NOI18N
        nomeSubestacao.setName("nomeSubestacao"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(bibliotecaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(palco, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(paineisComboList, 0, 407, Short.MAX_VALUE))
                            .addComponent(nomeSubestacao, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)))
                    .addComponent(menuRapido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(menuRapido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bibliotecaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                        .addComponent(nomeSubestacao)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(paineisComboList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(palco, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                        .addGap(46, 46, 46)))
                .addGap(0, 0, 0))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        jMenuItem1.setAction(actionMap.get("novaModelagem")); // NOI18N
        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        fileMenu.add(jMenuItem1);

        jMenuItem2.setAction(actionMap.get("abre")); // NOI18N
        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        fileMenu.add(jMenuItem2);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        jMenuItem3.setAction(actionMap.get("salvar")); // NOI18N
        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText(resourceMap.getString("jMenuItem3.text")); // NOI18N
        jMenuItem3.setName("jMenuItem3"); // NOI18N
        fileMenu.add(jMenuItem3);

        jSeparator2.setName("jSeparator2"); // NOI18N
        fileMenu.add(jSeparator2);

        jMenuItem4.setAction(actionMap.get("salvarComo")); // NOI18N
        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem4.setText(resourceMap.getString("jMenuItem4.text")); // NOI18N
        jMenuItem4.setName("jMenuItem4"); // NOI18N
        fileMenu.add(jMenuItem4);

        exitMenuItem.setAction(actionMap.get("sair")); // NOI18N
        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        jMenu3.setText(resourceMap.getString("jMenu3.text")); // NOI18N
        jMenu3.setName("jMenu3"); // NOI18N

        jMenuItem5.setAction(actionMap.get("editaNomeSubestacao")); // NOI18N
        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK));
        jMenuItem5.setText(resourceMap.getString("jMenuItem5.text")); // NOI18N
        jMenuItem5.setName("jMenuItem5"); // NOI18N
        jMenu3.add(jMenuItem5);

        menuBar.add(jMenu3);

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem6.setAction(actionMap.get("editaNomePainel")); // NOI18N
        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.SHIFT_MASK));
        jMenuItem6.setText(resourceMap.getString("jMenuItem6.text")); // NOI18N
        jMenuItem6.setName("jMenuItem6"); // NOI18N
        jMenu1.add(jMenuItem6);

        jSeparator3.setName("jSeparator3"); // NOI18N
        jMenu1.add(jSeparator3);

        jMenuItem7.setAction(actionMap.get("adicionarPainel")); // NOI18N
        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem7.setText(resourceMap.getString("jMenuItem7.text")); // NOI18N
        jMenuItem7.setName("jMenuItem7"); // NOI18N
        jMenu1.add(jMenuItem7);

        jMenuItem8.setAction(actionMap.get("excluirPainel")); // NOI18N
        jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DELETE, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem8.setText(resourceMap.getString("jMenuItem8.text")); // NOI18N
        jMenuItem8.setName("jMenuItem8"); // NOI18N
        jMenu1.add(jMenuItem8);

        jSeparator8.setName("jSeparator8"); // NOI18N
        jMenu1.add(jSeparator8);

        jMenuItem12.setAction(actionMap.get("ativarPainel")); // NOI18N
        jMenuItem12.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem12.setText(resourceMap.getString("jMenuItem12.text")); // NOI18N
        jMenuItem12.setName("jMenuItem12"); // NOI18N
        jMenu1.add(jMenuItem12);

        menuBar.add(jMenu1);

        jMenu2.setAction(actionMap.get("excluirComponente")); // NOI18N
        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N

        jMenuItem9.setAction(actionMap.get("editaNomeComponente")); // NOI18N
        jMenuItem9.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.SHIFT_MASK));
        jMenuItem9.setText(resourceMap.getString("jMenuItem9.text")); // NOI18N
        jMenuItem9.setName("jMenuItem9"); // NOI18N
        jMenu2.add(jMenuItem9);

        jSeparator6.setName("jSeparator6"); // NOI18N
        jMenu2.add(jSeparator6);

        jMenuItem10.setAction(actionMap.get("adicionarComponente")); // NOI18N
        jMenuItem10.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem10.setText(resourceMap.getString("jMenuItem10.text")); // NOI18N
        jMenuItem10.setName("jMenuItem10"); // NOI18N
        jMenu2.add(jMenuItem10);

        jMenuItem11.setAction(actionMap.get("excluirComponente")); // NOI18N
        jMenuItem11.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DELETE, 0));
        jMenuItem11.setText(resourceMap.getString("jMenuItem11.text")); // NOI18N
        jMenuItem11.setName("jMenuItem11"); // NOI18N
        jMenu2.add(jMenuItem11);

        jSeparator10.setName("jSeparator10"); // NOI18N
        jMenu2.add(jSeparator10);

        jMenuItem14.setAction(actionMap.get("editaDependencias")); // NOI18N
        jMenuItem14.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.SHIFT_MASK));
        jMenuItem14.setText(resourceMap.getString("jMenuItem14.text")); // NOI18N
        jMenuItem14.setName("jMenuItem14"); // NOI18N
        jMenu2.add(jMenuItem14);

        jMenuItem13.setAction(actionMap.get("tornaCompPrincipal")); // NOI18N
        jMenuItem13.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem13.setText(resourceMap.getString("jMenuItem13.text")); // NOI18N
        jMenuItem13.setName("jMenuItem13"); // NOI18N
        jMenu2.add(jMenuItem13);

        menuBar.add(jMenu2);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 750, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 580, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

	private void botPainelAtivoMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_botPainelAtivoMouseClicked
		// TODO add your handling code here:
	}// GEN-LAST:event_botPainelAtivoMouseClicked

	private void botCompPrincipalMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_botCompPrincipalMouseClicked
		// TODO add your handling code here:
	}// GEN-LAST:event_botCompPrincipalMouseClicked

	private void paineisComboListActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_paineisComboListActionPerformed
		// TODO add your handling code here:

		Painel painelSelecionado = (Painel) paineisComboList.getSelectedItem();

		

		if (painelSelecionado != null) {
			if (modSubEstacao.getPainelVisual(painelSelecionado) != null) {
				jDesktopPane1.moveToFront(modSubEstacao.getPainelVisual(
						painelSelecionado).getRepresPainel(visIconesAtivada));
				try {
					modSubEstacao.getPainelVisual(painelSelecionado)
							.getRepresPainel(visIconesAtivada).setMaximum(true);
					// painelAdicionado.getPainelJung().getRepresPainel().setVisible(true);
				} catch (PropertyVetoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				botPainelAtivo.setEnabled(!painelSelecionado.getAtivo());
			}
		}
	}// GEN-LAST:event_paineisComboListActionPerformed

	private void jButton11MouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jButton11MouseClicked
		// TODO add your handling code here:
	}// GEN-LAST:event_jButton11MouseClicked

	private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton11ActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jButton11ActionPerformed

	private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jButton1MouseClicked
		// TODO add your handling code here:

	}// GEN-LAST:event_jButton1MouseClicked

	private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jButton5MouseClicked
		// TODO add your handling code here:
	}// GEN-LAST:event_jButton5MouseClicked

	private void jButton9MouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jButton9MouseClicked
		// TODO add your handling code here:
	}// GEN-LAST:event_jButton9MouseClicked

	private void jButton10MouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jButton10MouseClicked
		// TODO add your handling code here:
	}// GEN-LAST:event_jButton10MouseClicked

	private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton6ActionPerformed
		// TODO add your handling code here:

	}// GEN-LAST:event_jButton6ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree bibliotecaComponentes;
    private javax.swing.JPanel bibliotecaPanel;
    private javax.swing.JButton botCompPrincipal;
    private javax.swing.JButton botPainelAtivo;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton9;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator10;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JToolBar.Separator jSeparator7;
    private javax.swing.JSeparator jSeparator8;
    private javax.swing.JToolBar.Separator jSeparator9;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JPanel menuRapido;
    private javax.swing.JLabel nomeSubestacao;
    private javax.swing.JComboBox paineisComboList;
    private javax.swing.JScrollPane palco;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

	private final Timer messageTimer;
	private final Timer busyIconTimer;
	private final Icon idleIcon;
	private final Icon[] busyIcons = new Icon[15];
	private int busyIconIndex = 0;

	private JDialog aboutBox;
}
